﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Specialized;
using System.Collections.ObjectModel;

//using System.Windows.Forms;

//using Microsoft.Win32;
//using WinForm=System.Windows.Forms;    // Elle corrige le probleme d'ambiguiter.




namespace Emessenger
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        Boolean SendFile = false;
        private string JID;
        //JID = "admin@elmagnifico"; //Test statique 
        private WODXMPPCOMLib.wodXMPPComClass wodXMPP1;

        //passage de l'instance de la connection a cette fenetre "pour ne pas avoir plusieur conexion a la fois"
        public Window2(string jid,WODXMPPCOMLib.wodXMPPComClass wodXMPP)
        {
            InitializeComponent();
            ///label1.Content=     [A modifier]
           
            wodXMPP1 = wodXMPP;
            wodXMPP1.IncomingMessage += new WODXMPPCOMLib._IwodXMPPComEvents_IncomingMessageEventHandler(wodXMPP1_IncomingMessage);


            
            wodXMPP1.FileTransferStart += new WODXMPPCOMLib._IwodXMPPComEvents_FileTransferStartEventHandler(wodXMPP1_FileTransferStart);


            //JID = "admin@elmagnifico"; //Test statique    
        }
//////////////////////////////////////////////////////////
        





//////////////////////////////////////////////////////////
        //Reception du message text
        void wodXMPP1_IncomingMessage(WODXMPPCOMLib.XMPPContact Contact, WODXMPPCOMLib.XMPPChatRoom ChatRoom, WODXMPPCOMLib.XMPPMessage Message)
        {

            textBox3.Text += Contact.Nick + ": " + Message.Text + "\r\n";
            
            //[Des Methode qui ne fonctionne pas]
            //textBox3.Select(textBox1.Text.Length, 0);
            //textBox3.scroll() = textBox3.maxScroll();
            //textBox3.ScrollToCaret();
            textBox3.ScrollToEnd();


            // a modifier pour qu il accepte le images,...
            /*
            FlowDocument myFlowDoc = new FlowDocument();

            // Add paragraphs to the FlowDocument.
            myFlowDoc.Blocks.Add(new Paragraph(new Run(Contact.Nick + ": " + Message.Text + "\r\n")));
            
            //RichTextBox myRichTextBox = new RichTextBox();

            // Add initial content to the RichTextBox.
            textBox3.Document = myFlowDoc;
            textBox3.ScrollToEnd();
            */
        }


        // [A implimenter ] cette methode permet de passer a la fin de la liste a chaque message reçut
        private void textBox3_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        {

        }

        private void entrer(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            // envoyer le message si l'utilisateur tape la boutton "Entrer"
            {
                JID = textBox2.Text;

                wodXMPP1.SendText(JID, textBox1.Text);
                textBox3.Text += "Me: " + textBox1.Text + "\r\n";
                textBox1.Text = "";

                textBox3.ScrollToEnd();
            }
        }

        //[A implimenter ]
        private void aLa_Fermeture(object sender, System.ComponentModel.CancelEventArgs e)
        {
           // TODO : a implimenter   
        }





        

        // Mais quel galaire de 15h.30 à 00h pour réaliser ça !!!
        // Cette méthode permet d'envoyer un fichier à un utilisateur 
        private void FileSend_Click(object sender, RoutedEventArgs e)
        {
            // méthode OpenFileDialog n'existe pas dans WPF d'ou il faut utiliser l'antienne méthode les Forms
            // Il faux specifier explecittement la bibliothèque à utiliser 

            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Multiselect = false;

            // Filtrer le type de fichiers
            // ofd.Filter = "Data Sources (*.txt, *.xml)|*.txt*;*.xml|All Files|*.*";

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] filePath = ofd.FileNames;
                string[] safeFilePath = ofd.SafeFileNames;

                //MessageBox.Show("Le fichier selectionner est : \n" + ofd.FileName); // Test si fonctonnel ou pas ! ;)


                // 
                wodXMPP1.SendFile(textBox2.Text,ofd.FileName, "");
                SendFile = true;
            }
        }


        private void drop_send(object sender, DragEventArgs e)
        {
            string[] fileNames = e.Data.GetData(DataFormats.FileDrop, true) as string[];

            if (fileNames.Length > 0)
            {
                //photoPath = fileNames[0];
                MessageBox.Show(fileNames[0]);
                //photoSrc.Source = new BitmapImage(new Uri(photoPath));
            }

            // Mark the event as handled, so the control's native Drop handler is not called.
            e.Handled = true;
        }


        void wodXMPP1_FileTransferStart(WODXMPPCOMLib.XMPPContact Contact, WODXMPPCOMLib.XMPPFile File, ref WODXMPPCOMLib.XMPPActionsEnum Action)
        {
            if (SendFile == true)
            {
                Action = WODXMPPCOMLib.XMPPActionsEnum.Allow;
                SendFile = false;
                // supprimer les cordoneés de fichier envoyer
                //wodXMPP1.SendFile(null, null, null);
            }

        }

        private void deplasser(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	this.DragMove();
        }

        

    }
}
